
Panels.Pricelist.AlreadyInit = false;

Panels.Pricelist.CalculateBuildingInit = function()
{
  if (this.AlreadyInit)
    return;
    
  var select = $("BuildingCalcSelect");
  var buildings = [];

  for (var building in Global.BuildingInfoList)
  {
    if (!Global.BuildingInfoList.hasOwnProperty(building))
      continue;

    buildings.push(building);
  }

  buildings.sort();

  for (var i = 0; i < buildings.length; i++)
  {
    new Element("option", { value: buildings[i], html: buildings[i] })
      .inject(select, "bottom");
  }

  this.AlreadyInit = true;
}

Panels.Pricelist.CalculateBuildingProfit = function()
{
  var buildings = $("BuildingCalcSelect");
  var level = $("BuildingCalcLevel");
  var value = level.get("value").toInt();
  
  if (isNaN(value))
    value = 1;

  level.set("value", value);
  level = value;

  var building = buildings.getSelected()[0].get("value");
  building = Utility.ObjectInfo.Get(eBuilding, building);
  building = new Building(null, null, null, building.name, null, level); // so we can use the calculate functions

  var cost = 0;
  var sales = 0;
  var profit = 0;

  var upkeep = building.calculateUpkeep();

  for (var commodity in upkeep)
  {
    if (!upkeep.hasOwnProperty(commodity))
      continue;

    cost += Utility.Pricelist.GetBuy(commodity) * upkeep[commodity];
  }

  var production = building.calculateProduction();

  for (var commodity in production)
  {
    if (!production.hasOwnProperty(commodity))
      continue;

    sales += Utility.Pricelist.GetSell(commodity) * production[commodity];
  }

  profit = sales - cost;

  var types = { "Tick": 1, "Day": 4, "Week": 28 };

  for (var type in types)
  {
    if (!types.hasOwnProperty(type))
      continue;

    $("BuildingCalcCost" + type).set("html", (cost * types[type]).addCommas());
    $("BuildingCalcSales" + type).set("html", (sales * types[type]).addCommas());
    $("BuildingCalcProfit" + type).set("html", (profit * types[type]).addCommas());
  }
}

Panels.Pricelist.CalculateTotals = function()
{
  var buildings = Utility.Object.GetList(eBuilding);

  var cost = 0;
  var sales = 0;
  var profit = 0;

  for (var i = 0; i < buildings.length; i++)
  {
    var building = buildings[i];
    var upkeep = building.calculateUpkeep();
    var production = building.calculateProduction();

    for (var commodity in upkeep)
    {
      if (!upkeep.hasOwnProperty(commodity))
        continue;

      cost += upkeep[commodity] * Utility.Pricelist.GetBuy(commodity);
    }

    for (var commodity in production)
    {
      if (!production.hasOwnProperty(commodity))
        continue;

      sales += production[commodity] * Utility.Pricelist.GetSell(commodity);
    }
  }

  profit = sales - cost;

  var types = { Tick: 1, Day: 4, Week: 28 };

  for (var type in types)
  {
    if (!types.hasOwnProperty(type))
      continue;

    $("SectorTotalCost" + type).set("html", (cost * types[type]).addCommas());
    $("SectorTotalSales" + type).set("html", (sales * types[type]).addCommas());
    $("SectorTotalProfit" + type).set("html", (profit * types[type]).addCommas());
  }
}

PricelistTabControl.addEvent("active", function(id)
{
  if (id == 1)
  {
    Panels.Pricelist.CalculateBuildingInit();
    Panels.Pricelist.CalculateBuildingProfit();
    Panels.Pricelist.CalculateTotals();
  }
});

MainTabControl.addEvent("active", function(id, section, tab)
{
  if (section.get("id") == "PricelistTab")
  {
    if (PricelistTabControl.now == 1)
    {
      Panels.Pricelist.CalculateBuildingInit();
      Panels.Pricelist.CalculateBuildingProfit();
      Panels.Pricelist.CalculateTotals();
    }
  }
});